#!/usr/bin/python
import serial
import time
from time import sleep
 
def chk_serial():
	ser = serial.Serial('/dev/ttyAMA0', 115200, timeout=0.5) 
	#ser.write('Send string by serial'.encode("gbk"))

	print("串口名称：", ser.name)
	print("端口号：", ser.port)
	print("波特率：", ser.baudrate)
	print("字节数：", ser.bytesize)
	print("校验位：", ser.parity)
	print("停止位：", ser.stopbits)
	print("读超时：", ser.timeout)
	print("写超时：", ser.writeTimeout)
	print("软件流控：", ser.xonxoff)
	print("硬件流控：", ser.rtscts)
	print("硬件流控：", ser.dsrdtr)
	#print("字符间隔超时：", interCharTimeout)
	ser.close()
 
def recv(serial):  
  while True:  
    data =serial.read(10)  #手动设置读取字符的数量为10
    if data == '':  
      continue
    else:
      break
    sleep(0.02) 
  return data 
 
def rec_tra():
    while True:
        count = ser.inWaiting() # 获得接收缓冲区字符
        if count != 0:
            recv = ser.read(count) # 读取内容
            ser.write(recv)        # 发送回PC
            print(recv)
        ser.flushInput()  # 清空接收缓冲区
        time.sleep(0.1)   # 必要的软件延时

if __name__ == '__main__':
    chk_serial()

	#while True:  
    #data = recv(ser) 
    #if data != '': 
    #    print(data)
    #    ser.write(data)

    #try:
    #    rec_tra()
    #except KeyboardInterrupt:
    #    if ser != None:
    #        ser.close()

